package com.yuxl.admin.manager.auth.server.controller;

import com.yuxl.admin.common.core.utils.R;
import com.yuxl.admin.manager.auth.core.entity.AuthInterface;
import com.yuxl.admin.manager.auth.core.entity.AuthRole;
import com.yuxl.admin.manager.auth.core.entity.AuthUser;
import com.yuxl.admin.manager.auth.core.repository.AuthInterfaceRepository;
import com.yuxl.admin.manager.auth.core.repository.AuthRolePowerRepository;
import com.yuxl.admin.manager.auth.core.repository.AuthRoleRepository;
import com.yuxl.admin.manager.auth.core.service.AuthInterfaceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author : yuxl
 * @create 2024/10/29 10:16
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/manager/interface")
@Tag(name = "接口管理", description = "接口的 CRUD 操作")
public class AuthInterfaceController {
    
    private final AuthInterfaceService authInterfaceService;
    /**
     * 创建新接口
     * @param entity 要创建的接口对象
     * @return 创建成功的接口对象
     */
    @PostMapping
    @Operation(summary = "创建新接口", description = "创建一个新的接口")
    public Mono<R<AuthInterface>> create(@RequestBody AuthInterface entity) {
        // 保存新接口到数据库
        entity.setCreateTime(LocalDateTime.now());
        return R.ok(authInterfaceService.create(entity));
    }

    /**
     * 根据ID获取接口
     * @param id 接口ID
     * @return 接口对象
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取接口", description = "根据ID获取接口信息")
    public Mono<R<AuthInterface>> getById(@PathVariable Long id) {
        return R.ok(authInterfaceService.getById(id));
    }

    /**
     * 更新接口信息
     * @param id 要更新的接口ID
     * @param entity 包含更新信息的接口对象
     * @return 更新后的接口对象
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新接口", description = "更新指定ID的接口信息")
    public Mono<R<AuthInterface>> update(@PathVariable Long id, @RequestBody AuthInterface entity) {
        return R.ok(authInterfaceService.update(id, entity));
    }

    /**
     * 删除接口
     * @param id 要删除的接口ID
     * @return 无返回值
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除接口", description = "删除指定ID的接口")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public Mono<R<Void>> delete(@PathVariable Long id) {
        return R.ok(authInterfaceService.delete(id));
        // 根据ID删除接口
    }

    /**
     * 获取所有接口
     * @return 所有接口的列表
     */
    @GetMapping
    @Operation(summary = "获取所有接口", description = "获取所有接口的列表")
    public Mono<R<List<AuthInterface>>> getAll() {
        return R.ok(authInterfaceService.getAll());
        // 获取所有接口
    }

    /**
     * 获取接口数量
     * @return 符合条件的接口数量
     */
    @GetMapping("/count")
    @Operation(summary = "获取接口数量", description = "获取符合条件的接口数量")
    public Mono<R<Long>> count() {
        return R.ok(authInterfaceService.count());
    }

    /**
     * 分页获取接口
     * @param page 页码
     * @param size 每页大小
     * @return 分页后的接口列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页获取接口", description = "分页获取接口列表")
    public Mono<R<List<AuthInterface>>> getPage(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return R.ok(authInterfaceService.getPage(page,size));
    }

    /**
     * 获取接口的角色
     * @param interfaceId 接口ID
     * @return 拥有指定接口的用户列表
     */
    @GetMapping("/roles")
    @Operation(summary = "获取接口对应的角色", description = "获取指定接口ID或接口标记的所有角色")
    public Mono<R<List<AuthRole>>> getRoles(
            @RequestParam(required = false) Long interfaceId) {
        return R.ok(authInterfaceService.getRoles(interfaceId));
    }
}
